import Vue from 'vue'
import Router from 'vue-router'

// 解决掉push相同路径时，报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    err
  })
}

Vue.use(Router)

/***************************
 * 合并子路由代码
 */
let routes = []
const routerContext = require.context('./', true, /index\.js$/)
routerContext.keys().forEach(route => {
  // 如果是根目录的 index.js、 不做任何处理         
  if (route.startsWith('./index')) {
    return;
  }
  const routerModule = routerContext(route)
  // 兼容 import export 和 require module.export 两种规范  Es modules  commonjs
  routes = [...routes, ...(routerModule.default || routerModule)]
})

//路由配置
import plat from './_plat/index';
import children from './children/index';
const router = new Router({
  routes: [
    {
      path: '/',
      name: 'Login',
      component: () => import('@/pages/Login')
    },
    {
      path: '/Index',
      name: 'Index',
      component: () => import('@/pages/Index'),
      children: [
        ...plat,
        ...children
      ]
    }
  ]
});

export default router